<?php

require_once(dirname(__FILE__).'/../../interfaces/IGcFilterFormPreparationHandler.interface.php');

/**
 * Klasa generująca elementy formularza służacego do aktywacji poszczególnych filtrów
 * 
 * @author strange3studio
 *
 */
class GcStandardFilterFormPreparationHandler implements IGcFilterFormPreparationHandler {
	
	// group id that need Filtration Form
	private $_gid;
	
	// tablica przechowująca aktualne ustawienia grupy odnośnie włączonych filtrów
	private $_panesEnabled;
	
	// zmienna mówiąca o tym ile jest aktywnych filtrów w grupi
  private $_panesEnabledCount;
	
  private function countEnabledPanes() {
  	foreach($this->_panesEnabled as $pane) {
  		if($pane) {
  			$this->_panesEnabledCount++;
  		}
  	}
  }
  
  private function filterPanes($gid) {

    $sql = db_query("SELECT filter_by_producer, filter_by_price_range, filter_by_price_sorting
                     FROM {gc_standard_filters} WHERE gnid=%d",$gnid);
    
    $result = db_fetch_object($sql);
    $filters_applied = array();
    if ($result) {
      $filters_applied['filter_by_producer'] = $result->filter_by_producer;
      $filters_applied['filter_by_price_range'] = $result->filter_by_price_range;     
      $filters_applied['filter_by_price_sorting'] = $result->filter_by_price_sorting;
    }
    else {
      $filters_applied['filter_by_producer'] = 1;
      $filters_applied['filter_by_price_range'] = 1;
      $filters_applied['filter_by_price_sorting'] = 1;  
    }
    return $filters_applied;
  }
  
	public function __construct($gid) {
		$this->_gid = $gid;
		$this->_panesEnabled = $this->filterPanes($gid);
		$this->_panesEnabledCount = 0;
		
		$this->countEnabledPanes();
	}
	public function generateForm() {
	 $form = array();
		/* 
	   * pobierz tablicę z filtrami, które zostały włączone dla tej treści
	   */
	   $standard_filters_applied = $this->_panesEnabled;

	   /* 
	    * budujemy formularz do wyboru ustawień standardowego filtrowania zawartości 
	    * katalogu grup wraz z przyciskiem do inicjalizowania filtracji
	    */
	   if ($standard_filters_applied['filter_by_producer']) {
	      $form['producent'] = add_gc_filters_producent_filter_pane();
	   }
	   if ($standard_filters_applied['filter_by_price_range']) {
	     $form['price_range'] = add_gc_filters_price_filter_pane();
	   }
	   if ($standard_filters_applied['filter_by_price_sorting']) {
	     $form['price_sorting'] = add_gc_filters_price_sorting_filter_pane();
	   }

	   
    return $form;
	}
	
	public function panesEnabledCount() {
		return $this->_panesEnabledCount;
	}
}